﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Merge
{
    public class FilterKeywordsManager
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public FilterKeywordsManager(ISet<string> keywords)
        {
            this.Keywords = keywords;
        }

        public FilterKeywordsManager(string filterKeywordsFile)
        {
            if (filterKeywordsFile != null && File.Exists(filterKeywordsFile))
            {
                Keywords = ReadKeywords(filterKeywordsFile);
                log.Info(this.ToString());
            }
        }

        public ISet<string> Keywords { get; set; } = new HashSet<string>();

        private static ISet<string> ReadKeywords(string filterKeywordsFile)
        {
            log.Info($"读入{filterKeywordsFile}");
            ISet<string> keywords = File.ReadAllText(filterKeywordsFile, Encoding.UTF8)
                .Split(new char[] { ' ', ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Trim())
                .ToHashSet();
            return keywords;
        }

        public ISet<string> CalcExclude(ISet<string> includes)
        {
            return this.Keywords.Except(includes).ToHashSet();
        }
        public override string ToString()
        {
            return string.Join(",", this.Keywords.OrderBy(x => x));
        }
    }
}
